package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.constant.JwtConstants;
import cn.stylefeng.guns.core.util.JwtTokenUtil;
import cn.stylefeng.guns.core.util.UserUtil;
import cn.stylefeng.guns.modular.api.entity.Task;
import cn.stylefeng.guns.modular.api.model.enums.TaskTypeEnum;
import cn.stylefeng.guns.modular.api.model.params.TaskParam;
import cn.stylefeng.guns.modular.api.model.result.TaskResult;
import cn.stylefeng.guns.modular.api.service.TaskService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import io.swagger.annotations.Api;

import javax.servlet.http.HttpServletRequest;


/**
 * 任务管理控制器
 *
 * @author wusonghe
 * @Date 2019-08-31 00:07:13
 */
@Controller
@Api(tags="任务管理")
@RequestMapping("/api/task")
public class TaskController extends BaseController {

    private String PREFIX = "/task";

    @Autowired
    private TaskService taskService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/task.html";
    }

    /**
     * 新增页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/task_add.html";
    }

    /**
     * 编辑页面
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/task_edit.html";
    }

    /**
     * 新增接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/addItem")
    @ResponseBody
    @ApiOperation(value = "新增接口", notes = "新增", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "新增", response = ResponseData.class)})
    public ResponseData addItem(TaskParam taskParam) {
        this.taskService.add(taskParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @PostMapping("/done")
    @ResponseBody
    @ApiOperation(value = "完成当前任务接口", notes = "完成当前任务", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "完成当前任务", response = ResponseData.class)})
    public ResponseData done(TaskParam taskParam) {

        this.taskService.done(taskParam);
        return ResponseData.success();
    }

    /**
     * 更新接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/editItem")
    @ResponseBody
    @ApiOperation(value = "更新接口", notes = "更新", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "更新", response = ResponseData.class)})
    public ResponseData editItem(TaskParam taskParam) {
        this.taskService.update(taskParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/delete")
    @ResponseBody
//    @ApiOperation(value = "删除接口", notes = "删除", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "删除", response = ResponseData.class)})
    public ResponseData delete(TaskParam taskParam) {
        this.taskService.delete(taskParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping("/detail")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = TaskResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = TaskResult.class)})
    public ResponseData detail(TaskParam taskParam) {
        TaskResult detail = this.taskService.findBySpec(taskParam);
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
	@PostMapping(value = "/list")
    @ResponseBody
    @ApiOperation(value = "列表接口", notes = "列表", response = TaskResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "列表", response = TaskResult.class)})
    public LayuiPageInfo list(TaskParam taskParam, HttpServletRequest request) {


        String nameAccount =  UserUtil.getNameAccountFromToken(request);
        if (nameAccount!=null){
            taskParam.setWorker(nameAccount);
        }
        return this.taskService.findPageBySpec(taskParam);
    }

    /**
     * 查看详情接口
     *
     * @author wusonghe
     * @Date 2019-08-31
     */
    @PostMapping("/undone")
    @ResponseBody
    @ApiOperation(value = "未完成接口", notes = "未完成", response = TaskResult.class)
    @ApiResponses({@ApiResponse(code = 200, message = "未读", response = TaskResult.class)})
    public ResponseData undone(TaskParam taskParam) {
        Integer count = this.taskService.unDone(taskParam);
        return ResponseData.success(count);
    }

}


